Android Studio 布局预览时 渲染错误

错误 Missing classes

1
2
3
4
5
6
7
Missing classes

The following classes could not be found:
- com.caiyi.youle.camera.ui.CameraFilterHorizontalScrollView (Fix Build Path, Edit XML, Create Class)
- com.caiyi.youle.camera.ui.FocusImageView (Fix Build Path, Edit XML, Create Class)
- com.caiyi.youle.camera.ui.VideoProgressView (Fix Build Path, Edit XML, Create Class)
Tip: Try to build the project. Tip: Try to refresh the layout

解决方法:去掉布局文件中,不存在的 viewModel 对象的引用。重新 build,就可以了。

error: Error: Dimension types not allowed (at ‘shadowDx’ with value ‘5dp’

1
2
3
4
5
6
7
8
9
10
11
12
13
<TextView
android:id="@+id/iv_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_filter_selector"
android:gravity="center"
android:padding="8dp"
android:text="@string/filter"
android:textSize="@dimen/font_small_size"
android:shadowColor="@color/black"
android:shadowDx="5dp"
android:shadowDy="5dp"
android:shadowRadius="10dp"/>

改了只后,还出错,而且,XML中的值被改回去。仔细看错误发现,错误指向的XML不是原始文件:
D:\\work\\videoshare_android.git\\app\\build\\intermediates\\data-binding-layout-out\\_360\\debug\\layout\\activity_camera_record_video_layout.xml

错误 Using the design library requires using Theme.AppCompat or a descendant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Using the design library requires using Theme.AppCompat or a descendant

Failed to instantiate one or more classes
android.support.design.widget.CoordinatorLayout
android.support.design.widget.AppBarLayout
android.support.design.widget.FloatingActionButton

one or more layouts are missing the layout_width or layout_height attributes

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:205)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:199)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:475)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:262)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:220)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:368)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:567)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:549)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:863)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:549)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$1(RenderTask.java:680)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

配置清单
解决方法
删除 PlayerActivity 的 android:theme="@style/AppTheme.NoActionBar"
即使用 <application> 指定的样式 android:theme="@style/AppTheme"
或者给 @style/AppTheme.NoActionBar 增加父样式 Theme.AppCompat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LansoDemoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PlayerActivity"
android:label="@string/title_activity_player"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>

@style/AppTheme.NoActionBar 和 @style/AppTheme 样式的代码

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

gradle.build 指定的依赖

1
2
3
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'

出错的 Activity 的定义

1
public class PlayerActivity extends AppCompatActivity

出错的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="work.wangxiang.lansodemo.PlayerActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_player" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

把 activity 样式回复成原来的,重新打开 AS,又不出错了。

错误:Layout fidelity warning

1
2
3
4
5
6
7
8
9
10
<TextView
android:id="@+id/iv_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_filter_selector"
android:gravity="center"
android:padding="8dp"
android:text="@string/filter"
android:textSize="@dimen/font_small_size"
android:shadowColor="@color/white" />

accurate
英 [ˈækjərət] 美 [ˈækjərɪt]
adj.精确的,准确的;正确无误的

fidelity
英 [fɪˈdeləti] 美 [fɪˈdɛlɪti,faɪ-]
n.保真度;逼真;忠诚,忠实;尽责

The graphics preview in the layout editor may not be accurate:
Paint.setShadowLayer is not supported
error-the-graphics-preview-in-the-layout-editor-may-not-be-accurate-paint-set
It means the preview doesn’t know how to implement setShadowLayer. This means the preview won’t look exactly like the result rendered on the device. Which is one of many reasons why you shouldn’t trust the preview app- always test your layouts on a physical device before assuming they’re done.